package com.soosync.common;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;

public class BaseException extends Exception {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8473144242440974243L;
	public BaseException() {
		isFirst = true;
	}

	public BaseException(String s) {
		super(s);
		isFirst = true;
	}

	public BaseException(String message, Throwable rootCause) {
		super(message);
		isFirst = true;
		this.rootCause = rootCause;
		isFirst = false;
	}

	public BaseException(Throwable rootCause) {
		this();
		this.rootCause = rootCause;
		isFirst = false;
	}

	public Throwable getRootCause() {
		return rootCause;
	}

	public String getStackTraceString() {
		StringWriter s = new StringWriter();
		printStackTrace(new PrintWriter(s));
		return s.toString();
	}

	public void printStackTrace() {
		printStackTrace(System.err);
	}

	public void printStackTrace(PrintStream s) {
		synchronized (s) {
			super.printStackTrace(s);
			if (rootCause != null)
				rootCause.printStackTrace(s);
			if (isFirst || !(rootCause instanceof BaseException))
				s.println("-----------------------------");
		}
	}

	public void printStackTrace(PrintWriter s) {
		synchronized (s) {
			super.printStackTrace(s);
			if (rootCause != null)
				rootCause.printStackTrace(s);
			if (isFirst || !(rootCause instanceof BaseException))
				s.println("-----------------------------");
		}
	}

	private Throwable rootCause;
	private boolean isFirst;
}
